{
 "cells": [
  {
   "cell_type": "code",
   "id": "d2d11d44-fa60-4e87-a1de-f393320551d9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-17T12:28:23.161852Z",
     "start_time": "2025-09-17T12:28:22.794416Z"
    }
   },
   "source": [
    "import json\n",
    "import os\n",
    "import dashscope\n",
    "from dashscope.api_entities.dashscope_response import Role\n",
    "\n",
    "# 从环境变量中，获取 DASHSCOPE_API_KEY\n",
    "#替换key\n",
    "api_key = \"sk-ad10ac04e97243c7abb74aa864d27331\"\n",
    "dashscope.api_key = api_key\n",
    "\n",
    "# 封装模型响应函数\n",
    "def get_response(messages):\n",
    "    # generation生成式，传入模型名称以及message就可以\n",
    "    response = dashscope.Generation.call(\n",
    "        model='qwen-turbo',# deepseek-v3\n",
    "        messages=messages,\n",
    "        #response[0]\n",
    "        result_format='message'  # 将输出设置为message形式\n",
    "    )\n",
    "    return response\n",
    "    \n",
    "review = '这款音效特别好 给你意想不到的音质。'\n",
    "\n",
    "messages=[\n",
    "    {\"role\": \"system\", \"content\": \"你是一名舆情分析师，帮我判断产品口碑的正负向，回复请用一个词语：正向 或者 负向\"},\n",
    "    {\"role\": \"user\", \"content\": review}\n",
    "  ]\n",
    "\n",
    "response = get_response(messages)\n",
    "response.output.choices[0].message.content"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'正向'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-17T12:31:27.202800Z",
     "start_time": "2025-09-17T12:31:26.625258Z"
    }
   },
   "cell_type": "code",
   "source": [
    "review = '这款音效特别好 给你意想不到的音质。'\n",
    "\n",
    "messages=[\n",
    "    {\"role\": \"system\", \"content\": \"你是一个心情鉴定大师判断一句话用开心还是不开心表示\"},\n",
    "    {\"role\": \"user\", \"content\": review}\n",
    "  ]\n",
    "\n",
    "response = get_response(messages)\n",
    "response.output.choices[0].message.content"
   ],
   "id": "21eb1ae52bbd009",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'开心'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-17T12:45:08.988286Z",
     "start_time": "2025-09-17T12:45:08.136941Z"
    }
   },
   "cell_type": "code",
   "source": [
    "messages.append({\"role\": \"user\", \"content\":\"这个音响不是很好\"})\n",
    "response = get_response(messages)\n",
    "response.output.choices[0].message.content"
   ],
   "id": "1a895fce8216b41",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'第一句：“这款音效特别好 给你意想不到的音质。” —— **开心**  \\n第二句：“这个音响不是很好” —— **不开心**'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-17T12:46:55.636246Z",
     "start_time": "2025-09-17T12:46:55.627998Z"
    }
   },
   "cell_type": "code",
   "source": "messages",
   "id": "744c32439cd8bd51",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'role': 'system', 'content': '你是一个心情鉴定大师判断一句话用开心还是不开心表示'},\n",
       " {'role': 'user', 'content': '这款音效特别好 给你意想不到的音质。'},\n",
       " {'role': 'user', 'content': '这个音响不是很好'}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-17T12:52:30.422710Z",
     "start_time": "2025-09-17T12:52:25.387625Z"
    }
   },
   "cell_type": "code",
   "source": [
    "review=\"帮我简单写一个两层的神经网络类\"\n",
    "messages=[\n",
    "    {\"role\": \"system\", \"content\": \"你是一个资深的程序员，直接回复代码，不要所无关的话，代码可以加注释\"},\n",
    "    {\"role\": \"user\", \"content\": review}\n",
    "  ]\n",
    "\n",
    "response = get_response(messages)\n",
    "content=response.output.choices[0].message.content\n",
    "print(content)"
   ],
   "id": "4ca54df62df94fda",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```python\n",
      "import numpy as np\n",
      "\n",
      "class NeuralNetwork:\n",
      "    def __init__(self, input_size, hidden_size, output_size):\n",
      "        # 初始化权重和偏置\n",
      "        self.weights_input_hidden = np.random.rand(input_size, hidden_size)\n",
      "        self.bias_hidden = np.zeros((1, hidden_size))\n",
      "        self.weights_hidden_output = np.random.rand(hidden_size, output_size)\n",
      "        self.bias_output = np.zeros((1, output_size))\n",
      "\n",
      "    def sigmoid(self, x):\n",
      "        return 1 / (1 + np.exp(-x))\n",
      "\n",
      "    def forward(self, X):\n",
      "        # 输入层到隐藏层\n",
      "        self.hidden_layer_in = np.dot(X, self.weights_input_hidden) + self.bias_hidden\n",
      "        self.hidden_layer_out = self.sigmoid(self.hidden_layer_in)\n",
      "\n",
      "        # 隐藏层到输出层\n",
      "        self.output_layer_in = np.dot(self.hidden_layer_out, self.weights_hidden_output) + self.bias_output\n",
      "        self.output = self.sigmoid(self.output_layer_in)\n",
      "\n",
      "        return self.output\n",
      "\n",
      "    def train(self, X, y, learning_rate=0.1, epochs=1000):\n",
      "        for _ in range(epochs):\n",
      "            # 前向传播\n",
      "            output = self.forward(X)\n",
      "\n",
      "            # 计算误差\n",
      "            error = y - output\n",
      "\n",
      "            # 反向传播\n",
      "            delta_output = error * output * (1 - output)\n",
      "            error_hidden = delta_output.dot(self.weights_hidden_output.T)\n",
      "            delta_hidden = error_hidden * self.hidden_layer_out * (1 - self.hidden_layer_out)\n",
      "\n",
      "            # 更新权重和偏置\n",
      "            self.weights_hidden_output += self.hidden_layer_out.T.dot(delta_output) * learning_rate\n",
      "            self.bias_output += np.sum(delta_output, axis=0, keepdims=True) * learning_rate\n",
      "            self.weights_input_hidden += X.T.dot(delta_hidden) * learning_rate\n",
      "            self.bias_hidden += np.sum(delta_hidden, axis=0, keepdims=True) * learning_rate\n",
      "```\n"
     ]
    }
   ],
   "execution_count": 9
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
